"""
ZK-6522C数控降压电源
"""
from machine import Pin, UART
import uasyncio as asyncio
import utime

class ZK6522C_STATUS:
    def __init__(self, hex_data):
        self.data = hex_data
        self.dev_addr = None
        self.dev_opt = None
        self.reg_cnt = None
        self.pwr_vout = None
        self.pwr_iout = None
        self.pwr_power = None
        self.pwr_uin = None
        self.pwr_ah = None
        self.pwr_wh = None
        self.pwr_out_h = None
        self.pwr_out_m = None
        self.pwr_out_s = None
        self.pwr_t_in = None
        self.pwr_t_ex = None
        self.pwr_lock = None
        self.pwr_protect = None
        self.pwr_cvcc = None
        self.pwr_onoff = None
        self.pwr_fc = None
        self.pwr_bled = None
        self.pwr_sleep = None
        self.pwr_model = None
        self.pwr_version = None
        self.pwr_slave_addr = None
        self.pwr_baudrate = None
        self.pwr_tin_offset = None
        self.pwr_tex_offset = None
        self.pwr_buzzer = None
        self.pwr_extact_m = None
        self.pwr_device = None
        
        self._parse_data(self.data)

    def _parse_data(self, data):
        self.data = data
        dev_addr_hex = data[:2]
        # print("dev_addr_hex:", dev_addr_hex)
        cut_bytes = bytes.fromhex(dev_addr_hex)
        # print("cut_bytes:", cut_bytes)
        self.dev_addr = ord(cut_bytes)
        #---------------------------------------
        dev_opt_hex = data[2:][:2]
        # print("dev_opt_hex:", dev_opt_hex)
        cut_bytes = bytes.fromhex(dev_opt_hex)
        # print("cut_bytes:", cut_bytes)
        self.dev_opt = ord(cut_bytes)
        #----------------------------------------
        reg_cnt_hex = data[4:][:2]
        # print("reg_cnt_hex:", reg_cnt_hex)
        cut_bytes = bytes.fromhex(reg_cnt_hex)
        # print("cut_bytes:", cut_bytes)
        self.reg_cnt = ord(cut_bytes)
        # vout------------------------------------
        pwr_vout_hex = data[6:][:4]
        # print("pwr_vout_hex:", pwr_vout_hex)
        cut_bytes = bytes.fromhex(pwr_vout_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_vout = int.from_bytes(cut_bytes, "big")
        self.pwr_vout = round(self.pwr_vout / 10 ** 2, 2)
        # iout-------------------------------------
        pwr_iout_hex = data[10:][:4]
        # print("pwr_iout_hex:", pwr_iout_hex)
        cut_bytes = bytes.fromhex(pwr_iout_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_iout = int.from_bytes(cut_bytes, "big")
        self.pwr_iout = round(self.pwr_iout / 10 ** 2, 2)
        # power------------------------------------
        pwr_power_hex = data[14:][:4]
        # print("pwr_power_hex:", pwr_power_hex)
        cut_bytes = bytes.fromhex(pwr_power_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_power = int.from_bytes(cut_bytes, "big")
        self.pwr_power = round(self.pwr_power / 10 ** 1, 1)
        # uin
        pwr_uin_hex = data[18:][:4]
        # print("pwr_uin_hex:", pwr_uin_hex)
        cut_bytes = bytes.fromhex(pwr_uin_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_uin = int.from_bytes(cut_bytes, "big")
        self.pwr_uin = round(self.pwr_uin / 10 ** 2, 2)
        
        pwr_ah_hex = data[22:][:8]
        # print("pwr_ah_hex:", pwr_ah_hex)
        cut_bytes = bytes.fromhex(pwr_ah_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_ah = int.from_bytes(cut_bytes, "big")
        
        pwr_wh_hex = data[30:][:8]
        # print("pwr_wh_hex:", pwr_wh_hex)
        cut_bytes = bytes.fromhex(pwr_wh_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_wh = int.from_bytes(cut_bytes, "big")
        
        pwr_out_h_hex = data[38:][:4]
        # print("pwr_out_h_hex:", pwr_out_h_hex)
        cut_bytes = bytes.fromhex(pwr_out_h_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_out_h = int.from_bytes(cut_bytes, "big")
        
        pwr_out_m_hex = data[42:][:4]
        # print("pwr_out_m:", pwr_out_m_hex)
        cut_bytes = bytes.fromhex(pwr_out_m_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_out_m = int.from_bytes(cut_bytes, "big")
        
        pwr_out_s_hex = data[46:][:4]
        #print("pwr_out_s:", pwr_out_s_hex)
        cut_bytes = bytes.fromhex(pwr_out_s_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_out_s = int.from_bytes(cut_bytes, "big")
        
        pwr_t_in_hex =  data[50:][:4]
        # print("pwr_t_in_hex:", pwr_t_in_hex)
        cut_bytes = bytes.fromhex(pwr_t_in_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_t_in = int.from_bytes(cut_bytes, "big")
        self.pwr_t_in = round(self.pwr_t_in / 10 ** 1, 1)
        
        pwr_t_ex_hex =  data[54:][:4]
        # print("pwr_t_ex_hex:", pwr_t_ex_hex)
        cut_bytes = bytes.fromhex(pwr_t_ex_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_t_ex = int.from_bytes(cut_bytes, "big")
        self.pwr_t_ex = round(self.pwr_t_ex / 10 ** 1, 1)
        
        pwr_lock_hex =  data[58:][:4]
        # print("pwr_lock_hex:", pwr_lock_hex)
        cut_bytes = bytes.fromhex(pwr_lock_hex)
        # print("cut_bytes:", cut_bytes)
        pwr_lock_val = int.from_bytes(cut_bytes, "big")
        if pwr_lock_val == 0:
            self.pwr_lock = False
        else:
            self.pwr_lock = True
        
        pwr_protect_hex = data[62:][:4]
        # print("pwr_protect_hex:", pwr_protect_hex)
        cut_bytes = bytes.fromhex(pwr_protect_hex)
        # print("cut_bytes:", cut_bytes)
        pwr_protect_val = int.from_bytes(cut_bytes, "big")
        protect_status_names = ["RUN", "OVP", "OCP", "OPP", "LVP", "OAH", "OHP", "OTP", "OEP", "OWH", "ICP", "IVP"]
        self.pwr_protect = protect_status_names[pwr_protect_val]
        
        pwr_cvcc_hex = data[66:][:4]
        # print("pwr_cvcc_hex:", pwr_cvcc_hex)
        cut_bytes = bytes.fromhex(pwr_cvcc_hex)
        # print("cut_bytes:", cut_bytes)
        pwr_cvcc_val = int.from_bytes(cut_bytes, "big")
        cvcc_names = ["CV", "CC"]
        self.pwr_cvcc = cvcc_names[pwr_cvcc_val]
        
        pwr_onoff_hex = data[70:][:4]
        # print("pwr_onoff_hex:", pwr_onoff_hex)
        cut_bytes = bytes.fromhex(pwr_onoff_hex)
        # print("cut_bytes:", cut_bytes)
        pwr_onoff_val = int.from_bytes(cut_bytes, "big")
        onoff_names = ["OFF", "ON"]
        self.pwr_onoff = onoff_names[pwr_onoff_val]
        
        pwr_fc_hex = data[74:][:4]
        # print("pwr_fc_hex:", pwr_fc_hex)
        cut_bytes = bytes.fromhex(pwr_fc_hex)
        # print("cut_bytes:", cut_bytes)
        pwr_fc_val = int.from_bytes(cut_bytes, "big")
        fc_names = ["C", "F"]
        self.pwr_fc = fc_names[pwr_fc_val]
        
        pwr_bled_hex = data[78:][:4]
        # print("pwr_bled_hex:", pwr_bled_hex)
        cut_bytes = bytes.fromhex(pwr_bled_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_bled = int.from_bytes(cut_bytes, "big")
        
        pwr_sleep_hex = data[82:][:4]
        # print("pwr_sleep_hex:", pwr_sleep_hex)
        cut_bytes = bytes.fromhex(pwr_sleep_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_sleep = int.from_bytes(cut_bytes, "big")
        
        pwr_model_hex = data[86:][:4]
        # print("pwr_model_hex:", pwr_model_hex)
        cut_bytes = bytes.fromhex(pwr_model_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_model = int.from_bytes(cut_bytes, "big")
        
        pwr_version_hex = data[90:][:4]
        # print("pwr_version_hex:", pwr_version_hex)
        cut_bytes = bytes.fromhex(pwr_version_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_version = int.from_bytes(cut_bytes, "big")
        
        pwr_slave_addr_hex = data[94:][:4]
        # print("pwr_slave_addr_hex:", pwr_slave_addr_hex)
        cut_bytes = bytes.fromhex(pwr_slave_addr_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_slave_addr = int.from_bytes(cut_bytes, "big")
        
        pwr_baudrate_hex = data[98:][:4]
        # print("pwr_baudrate_hex:", pwr_baudrate_hex)
        cut_bytes = bytes.fromhex(pwr_baudrate_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_baudrate = int.from_bytes(cut_bytes, "big")
        
        pwr_tin_offset_hex = data[102:][:4]
        # print("pwr_tin_offset_hex:", pwr_tin_offset_hex)
        cut_bytes = bytes.fromhex(pwr_tin_offset_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_tin_offset = int.from_bytes(cut_bytes, "big")
        self.pwr_tin_offset = round(self.pwr_tin_offset / 10 ** 1, 1)
        
        pwr_tex_offset_hex = data[106:][:4]
        # print("pwr_tex_offset_hex:", pwr_tex_offset_hex)
        cut_bytes = bytes.fromhex(pwr_tex_offset_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_tex_offset = int.from_bytes(cut_bytes, "big")
        self.pwr_tex_offset = round(self.pwr_tex_offset / 10 ** 1, 1)
        
        pwr_buzzer_hex = data[110:][:4]
        # print("pwr_buzzer_hex:", pwr_buzzer_hex)
        cut_bytes = bytes.fromhex(pwr_buzzer_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_buzzer = int.from_bytes(cut_bytes, "big")
        
        pwr_extract_m_hex = data[114:][:4]
        # print("pwr_extract_m_hex:", pwr_extract_m_hex)
        cut_bytes = bytes.fromhex(pwr_extract_m_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_extact_m = int.from_bytes(cut_bytes, "big")
        
        pwr_device_hex = data[118:][:4]
        # print("pwr_device_hex:", pwr_device_hex)
        cut_bytes = bytes.fromhex(pwr_device_hex)
        # print("cut_bytes:", cut_bytes)
        self.pwr_device = int.from_bytes(cut_bytes, "big")
        
        
    def show_status(self):
        """
        dev_addr: 1
        dev_opt: 3
        reg_cnt: 58
        pwr_vout: 5.0 V
        pwr_iout: 0.0 A
        pwr_power: 0.0 W
        pwr_uin: 34.33 V
        pwr_ah: 0 maH
        pwr_wh: 0 mwH
        pwr_out_h: 8 H
        pwr_out_m: 30 M
        pwr_out_s: 29 S
        pwr_t_in: 30.0 C
        pwr_t_ex: 0.0 C
        pwr_lock: False
        pwr_protect: RUN
        pwr_cvcc: CV
        pwr_onoff: ON
        pwr_fc: C
        pwr_bled: 5
        pwr_sleep: 1 M
        pwr_model: 6522
        pwr_version: 100
        pwr_slave_addr: 1
        pwr_baudrate: 6
        pwr_tin_offset: 0.0
        pwr_tex_offset: 0.0
        pwr_buzzer: 1
        pwr_extract_m: 0
        pwr_device: 2
        """
        print("dev_addr:", self.dev_addr)
        print("dev_opt:", self.dev_opt)
        print("reg_cnt:", self.reg_cnt)
        print("pwr_vout:", self.pwr_vout, "V")
        print("pwr_iout:", self.pwr_iout, "A")
        print("pwr_power:", self.pwr_power, "W")
        print("pwr_uin:", self.pwr_uin,"V")
        print("pwr_ah:", self.pwr_ah, "maH")
        print("pwr_wh:", self.pwr_wh, "mwH")
        print("pwr_out_h:", self.pwr_out_h, "H")
        print("pwr_out_m:", self.pwr_out_m, "M")
        print("pwr_out_s:", self.pwr_out_s, "S")
        print("pwr_t_in:", self.pwr_t_in, "C")
        print("pwr_t_ex:", self.pwr_t_ex, "C")
        print("pwr_lock:", self.pwr_lock)
        print("pwr_protect:", self.pwr_protect)
        print("pwr_cvcc:", self.pwr_cvcc)
        print("pwr_onoff:", self.pwr_onoff)
        print("pwr_fc:", self.pwr_fc)
        print("pwr_bled:", self.pwr_bled)
        print("pwr_sleep:", self.pwr_sleep, "M")
        print("pwr_model:", self.pwr_model)
        print("pwr_version:", self.pwr_version)
        print("pwr_slave_addr:", self.pwr_slave_addr)
        print("pwr_baudrate:", self.pwr_baudrate)
        print("pwr_tin_offset:", self.pwr_tin_offset)
        print("pwr_tex_offset:", self.pwr_tex_offset)
        print("pwr_buzzer:", self.pwr_buzzer)
        print("pwr_extract_m:", self.pwr_extact_m)
        print("pwr_device:", self.pwr_device)
        

class ZK6522C_PRESETS:
    
    def __init__(self, hex_data):
        self.data = hex_data
        self.dev_addr = None
        self.dev_opt = None
        self.reg_cnt = None
        self.s_vset = None
        self.s_iset = None
        self.s_lvp = None
        self.s_ovp = None
        self.s_ocp = None
        self.s_opp = None
        self.s_ohp_h = None
        self.s_ohp_m = None
        self.s_oah = None
        self.s_owh = None
        self.s_otp = None
        self.s_ini = None
        self._parse_data(self.data)
    
    def _parse_data(self, data):
        # 01|031a01f40898024419c808fc36b00000000100010000000100000258
        self.data = data
        # addr:01----------------------------------
        # 01|031a01f40898024419c808fc36b00000000100010000000100000258
        dev_addr_hex = data[:2]
        # print("dev_addr_hex:", dev_addr_hex)
        cut_bytes = bytes.fromhex(dev_addr_hex)
        # print("cut_bytes:", cut_bytes)
        self.dev_addr = ord(cut_bytes)
        # opt:03---------------------------------------
        # 01|03|1a01f40898024419c808fc36b00000000100010000000100000258
        dev_opt_hex = data[2:][:2]
        # print("dev_opt_hex:", dev_opt_hex)
        cut_bytes = bytes.fromhex(dev_opt_hex)
        # print("cut_bytes:", cut_bytes)
        self.dev_opt = ord(cut_bytes)
        # cnt:26----------------------------------------
        # 0103|1a|01f40898024419c808fc36b00000000100010000000100000258
        reg_cnt_hex = data[4:][:2]
        # print("reg_cnt_hex:", reg_cnt_hex)
        cut_bytes = bytes.fromhex(reg_cnt_hex)
        # print("cut_bytes:", cut_bytes)
        self.reg_cnt = ord(cut_bytes)
        # vset:500------------------------------------
        # 01031a|01f4|0898024419c808fc36b00000000100010000000100000258
        vset_hex = data[6:][:4]
        cut_bytes = bytes.fromhex(vset_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_vset = int.from_bytes(cut_bytes, "big")
        self.s_vset = round(self.s_vset / 10 ** 2, 2)
        # iset:2200------------------------------------
        # 01031a01f4|0898|024419c808fc36b00000000100010000000100000258
        iset_hex = data[10:][:4]
        cut_bytes = bytes.fromhex(iset_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_iset = int.from_bytes(cut_bytes, "big")
        self.s_iset = round(self.s_iset / 10 ** 2, 2)
        # s_lvp:580------------------------------------
        # 01031a01f40898|0244|19c808fc36b00000000100010000000100000258
        slvp_hex = data[14:][:4]
        cut_bytes = bytes.fromhex(slvp_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_lvp = int.from_bytes(cut_bytes, "big")
        self.s_lvp = round(self.s_lvp / 10 ** 2, 2)
        # s_ovp:6600------------------------------------
        # 01031a01f408980244|19c8|08fc36b00000000100010000000100000258
        sovp_hex = data[18:][:4]
        cut_bytes = bytes.fromhex(sovp_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_ovp = int.from_bytes(cut_bytes, "big")
        self.s_ovp = round(self.s_ovp / 10 ** 2, 2)
        # s_ocp:2300------------------------------------
        # 01031a01f40898024419c8|08fc|36b00000000100010000000100000258
        socp_hex = data[22:][:4]
        cut_bytes = bytes.fromhex(socp_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_ocp = int.from_bytes(cut_bytes, "big")
        self.s_ocp = round(self.s_ocp / 10 ** 2, 2)
        # s_opp:14000------------------------------------
        # 01031a01f40898024419c808fc|36b0|0000000100010000000100000258
        sopp_hex = data[26:][:4]
        cut_bytes = bytes.fromhex(sopp_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_opp = int.from_bytes(cut_bytes, "big")
        self.s_opp = round(self.s_opp / 10 ** 1, 1)
        # s_ohp_h:0------------------------------------
        # 01031a01f40898024419c808fc36b0|0000|000100010000000100000258
        sohp_h_hex = data[30:][:4]
        # print("pwr_out_h_hex:", pwr_out_h_hex)
        cut_bytes = bytes.fromhex(sohp_h_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_ohp_h = int.from_bytes(cut_bytes, "big")
        # s_ohp_m:1------------------------------------
        # 01031a01f40898024419c808fc36b00000|0001|00010000000100000258
        sohp_m_hex = data[34:][:4]
        # print("pwr_out_h_hex:", pwr_out_h_hex)
        cut_bytes = bytes.fromhex(sohp_m_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_ohp_m = int.from_bytes(cut_bytes, "big")
        # s_oah:65536------------------------------------
        # 01031a01f40898024419c808fc36b000000001|00010000|000100000258
        soah_hex = data[38:][:8]
        # print("pwr_out_m:", pwr_out_m_hex)
        cut_bytes = bytes.fromhex(soah_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_oah = int.from_bytes(cut_bytes, "big")
        # s_owh:65536------------------------------------
        # 01031a01f40898024419c808fc36b00000000100010000|00010000|0258
        sowh_hex = data[46:][:8]
        # print("pwr_out_m:", pwr_out_m_hex)
        cut_bytes = bytes.fromhex(sowh_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_owh = int.from_bytes(cut_bytes, "big")
        # s_otp:600------------------------------------
        # 01031a01f40898024419c808fc36b00000000100010000|00010000|0258
        sotp_hex = data[54:][:4]
        # print("pwr_out_m:", pwr_out_m_hex)
        cut_bytes = bytes.fromhex(sotp_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_otp = int.from_bytes(cut_bytes, "big")
        # s_ini:------------------------------------
        # ------------------------------------------------------0258
        sini_hex = data[58:][:4]
        # print("pwr_out_m:", pwr_out_m_hex)
        cut_bytes = bytes.fromhex(sini_hex)
        # print("cut_bytes:", cut_bytes)
        self.s_ini = int.from_bytes(cut_bytes, "big")
        

    def show_status(self):
        print("dev_addr:", self.dev_addr)
        print("dev_opt:", self.dev_opt)
        print("reg_cnt:", self.reg_cnt)
        print("s_vset:", self.s_vset)
        print("s_iset:", self.s_iset)
        print("s_lvp:", self.s_lvp)
        print("s_ovp:", self.s_ovp)
        print("s_ocp:", self.s_ocp)
        print("s_opp:", self.s_opp)
        print("s_ohp_h:", self.s_ohp_h)
        print("s_ohp_m:", self.s_ohp_m)
        print("s_oah:", self.s_oah)
        print("s_owh:", self.s_owh)
        print("s_otp:", self.s_otp)
        print("s_ini:", self.s_ini)
        

class ZK6522C:
    
    def __init__(self, uart):
        print("-" * 36)
        print("init ZK6522C Adj Power model...")
        self.uart = uart

        print("-" * 36)
        
    

    
    def uart_reader(self):
        """接收uart返回的数据"""
        ret = None
        while True:
            if self.uart.any():
                ret = self.uart.read()
                break 
            utime.sleep_ms(10)
        #print("zk_uart.received:", ret, "type:", type(ret))
        return ret.hex()
    
    def calculate_crc(self, hex_str_data):
        """计算hex数据的CRC"""
        crc = 0xFFFF
        data = bytearray.fromhex(hex_str_data)
        for byte in data:
            low_byte = crc & 0xFF
            crc = crc & 0xFF00 + low_byte ^ byte
            # print("byte:", hex(byte), "crc:", bin(crc), hex(crc))
            for _ in range(8):
                if (crc & 0x1):  # 检查右移后的最高位是否为 1（移出位）
                    crc = (crc >> 1)
                    crc ^= 0xA001    # 多项式 A001
                else:
                    crc = (crc >> 1)
        crc = ((crc >> 8) & 0xFF) | ((crc << 8) & 0xFF00)
        return hex(crc)[2:]
    
    def _send_command(self, data):
        crc = self.calculate_crc(data)
        cmd = data + crc
        buf = bytes.fromhex(cmd)
        self.uart.write(buf)
#         print("zk_uart.send cmd:", cmd)
        rsv = self.uart_reader()
        rsv_data = rsv[:-4]
        rsv_crc = rsv[-4:]
        rsv_data_crc = self.calculate_crc(rsv_data)
        # print("rsv:", rsv, "rsv_data:", rsv_data, "rsv_crc:", rsv_crc, "rsv_data_crc:", rsv_data_crc)
        if rsv_crc != rsv_data_crc:
            raise Exception("received data crc error.")
        return rsv_data
        
    def sendcmd_Read_Status(self):
        """ 0002H - 001EH, len=29regs 读设备状态"""
        data = "01030002001D"
        rsv = self._send_command(data)
        # print("rsv:", rsv)
        return rsv    

    def sendcmd_Read_VSet(self):
        """ 0000H 2byte 读设置输出电压"""
        data = "010300000002"
        self._send_command(data)
        
    def sendcmd_Write_VSet(self, val):
        """ 0000H 2byte 写设置输出电压"""
        data = "010600000002"
        self._send_command(data)

    def sendcmd_Read_ISet(self):
        """ 0001H 2byte 读设置输出电流"""
        data = "010300010002"
        self._send_command(data)
        
    def sendcmd_Write_ISet(self, val):
        """ 0001H 2byte 写设置输出电流"""
        data = "010600010002"
        self._send_command(data)
    
    def sendcmd_Write_Lock(self):
        """ 000FH LOCK 2byte 按键锁
        # 0001 lock
        # 0000 unlock"""
        data = "0106000F0001"
        self._send_command(data)
        
    def sendcmd_Write_UnLock(self):
        """ 000FH LOCK 2byte 按键解锁
        # 0001 lock
        # 0000 unlock"""
        data = "0106000F0000"
        self._send_command(data)
    
    def sendcmd_Write_ON(self):
        """ 0012H ONOFF 2byte 开启输出
        # 0000 OFF
        # 0001 ON"""
        data = "010600120001"
        self._send_command(data)
        
    def sendcmd_Write_OFF(self):
        """ 0012H ONOFF 2byte 关闭输出
        # 0000 OFF
        # 0001 ON"""
        data = "010600120000"
        self._send_command(data)
    
    def sendcmd_Read_TEMPFC(self):
        """ 0013H Temp F/C 2byte 读取温度单位F/C"""
        data = "010300130002"
        self._send_command(data)
   
    def sendcmd_Write_BLED(self, level):
        """ 0014H B-LED 2byte 0-5 写背光亮度"""
        data = "010600140005"
        self._send_command(data)
    
    def sendcmd_Write_SLEEP(self, minute):
        """ 0015H SLEEP 2byte 写息屏时间M"""
        data = "010300150002"
        self._send_command(data)
    
    def sendcmd_Write_SLAVE_ADD(self, addr):
        """ 0018H SLAVE ADD 2byte 写从机地址"""
        data = "010600180003"
        self._send_command(data)
        
    def sendcmd_Write_BAUDRATE(self):
        """ 0019H BAUDRATE 2byte 写波特率"""
        data = "010600192D00"
        self._send_command(data)
        
    def sendcmd_Write_BUZZER(self, val):
        """ 001CH BUZZER 2byte 写蜂鸣器开关"""
        data = "0106001C0001"
        self._send_command(data)
    
    def sendcmd_Write_Temp_IN_OFFSET(self, val):
        """ 001AH T IN OFFSET 2byte 写内部温度校正"""
        data = "0106001A0000"
        self._send_command(data)
    
    def sendcmd_Write_Temp_OUT_OFFSET(self, val):
        """ 001BH T OUT OFFSET 2byte 写外部温度校正"""
        data = "0106001B0000"
        self._send_command(data)
    
    def sendcmd_Read_SaveData(self, index):
        """读指定序号的预设数组 index m0-m10共11组"""
        addr = "01"
        opt  = "03"
        reg_start_hex= "00" + hex(0x50 + index * 0x0010)[2:]
        data = addr + opt + reg_start_hex + "0010"
        #print("read presets cmd:", data)
        rsv = self._send_command(data)
        #print("read presets data:", rsv)
        return rsv
    
    def sendcmd_Write_SaveData(self, index, hexvalstr):
        addr = "01"
        opt = "10"
        reg_start_hex= "00" + hex(0x50 + index * 0x0010)[2:]
        reg_cnt = "0E"
        write_bytes = "1C"
        data = addr + opt + reg_start_hex + reg_cnt + write_bytes + hexvalstr
        print("write save index:", index, "data:", data)
        self._send_command(data)
    
    def convert_val_hex(self, val, digits, hex_len=4):
        v = int(val * (10 ** digits))
        res = hex(v)[2:]
        l = len(res)
        if l >= hex_len:
            return res[hex_len:]
        else:
            t = "0" * (hex_len - l)
            return t + res
            
    
    
    def build_save_data(self,
                        v_set, i_set,
                        s_lvp, s_ovp,
                        s_ocp, s_opp,
                        s_ohp, s_oah, s_owh,
                        s_otp, s_ini):
        v_set_hex = self.convert_val_hex(v_set, 2)
        #print("v_set_hex:", v_set, v_set_hex)
        i_set_hex = self.convert_val_hex(i_set, 2)
        #print("i_set_hex:", i_set, i_set_hex)
        s_lvp_hex = self.convert_val_hex(s_lvp, 2)
        #print("s_lvp_hex:", s_lvp, s_lvp_hex)
        s_ovp_hex = self.convert_val_hex(s_ovp, 2)
        #print("s_ovp_hex:", s_ovp, s_ovp_hex)
        s_ocp_hex = self.convert_val_hex(s_ocp, 2)
        #print("s_ocp_hex:", s_ocp, s_ocp_hex)
        s_opp_hex = self.convert_val_hex(s_opp, 1)
        #print("s_opp_hex:", s_opp, s_opp_hex)
        s_ohp_h = int(s_ohp)
        s_ohp_m = (s_ohp - s_ohp_h) * 60
        s_ohp_h_hex = self.convert_val_hex(s_ohp_h, 0)
        #print("s_ohp_h_hex:", s_ohp_h, s_ohp_h_hex)
        s_ohp_m_hex = self.convert_val_hex(s_ohp_m, 0)
        #print("s_ohp_m_hex:", s_ohp_m, s_ohp_m_hex)
        s_oah_hex = self.convert_val_hex(s_oah, 0, 8)
        s_oah_l_hex = s_oah_hex[:4]
        s_oah_h_hex = s_oah_hex[4:][:4]
        #print("s_oah_hex:", s_oah, s_oah_hex, s_oah_l_hex, s_oah_h_hex)
        s_owh_hex = self.convert_val_hex(s_owh, 0, 8)
        s_owh_l_hex = s_owh_hex[:4]
        s_owh_h_hex = s_owh_hex[4:][:4]
        #print("s_owh_hex:", s_owh, s_owh_hex, s_owh_l_hex, s_owh_h_hex)
        s_otp_hex = self.convert_val_hex(s_otp, 1)
        #print("s_otp_hex:", s_otp, s_otp_hex)
        s_ini_hex = self.convert_val_hex(s_ini, 0)
        #print("s_ini_hex:", s_ini, s_ini_hex)
        s_o_close_hex = "0000"
        data = ""
        data += v_set_hex
        data += i_set_hex
        data += s_lvp_hex
        data += s_ovp_hex
        data += s_ocp_hex
        data += s_opp_hex
        data += s_ohp_h_hex
        data += s_ohp_m_hex
        data += s_oah_l_hex
        data += s_oah_h_hex
        data += s_owh_l_hex
        data += s_owh_h_hex
        data += s_otp_hex
        data += s_ini_hex
        data += s_o_close_hex
        return data
    
    def sendcmd_Write_Set_Factory(self):
        " 0020H 2byte Rest Factory 恢复出厂设置"
        data = "010600200001"
        self._send_command(data)
        
    def sendcmd_Write_Clean_Data(self):
        " 0021H 2byte Zero 清零"
        data = "010600210001"
        self._send_command(data)
        
    def sendcmd_Write_Reset(self):
        " 002FH 2byte Reset 重启"
        data = "0106002F0001"
        self._send_command(data)
        
   

if __name__ == "__main__":
    tx = 18
    rx = 19
    uart = UART(1, baudrate=115200, tx=Pin(tx), rx=Pin(rx))
    def read_data_callback(ZK6522C_STATUS:data):
        data.show_status()
    
    zk = ZK6522C(uart)
    rsv = zk.sendcmd_Read_Status()
    st = ZK6522C_STATUS(rsv)
    #st.show_status()
    save_data = zk.sendcmd_Read_SaveData(0)
    print("set_data 0:", save_data)
    
    

        
    #if not flag:
    #    cmd = "01030002000265cb"
    #    buf = bytes.fromhex(cmd)
    #    print("write buf:", buf, "type:", type(buf), "cmd:", cmd)
    #    uart.write(buf)
    #    flag = True
    #    utime.sleep_ms(10)
    #    if uart.any():
    #        data = uart.read()
    #        print("ZK_Uart.received:", data, "type:", type(data))
        #utime.sleep_ms(1000)
    #zk.sendcmd_Read_SaveData(10)
    #cnf_data = zk.build_save_data(5.55, 2.22, 4.80, 5.88, 2.88, 10.1, 8.5, 5000, 5000, 60.5, 0)
    #print("cnf_data:", cnf_data)




